<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kuaijiefee/model')->方法名();
*/
namespace jy_kuaijiefee;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 1, count($rows) - 1);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传excel文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel2($localexcel);
		//print_r($excel);exit;
		//$excel[0]=$excel[1];
		//unset($excel[1]);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['核销门店ID'] && !$res[0]['订单ID']){
			$excel=cfc('tools')->importExcel2($localexcel,true);
			$excel[1]=$excel[2];
			unset($excel[2]);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['核销门店ID'] && !$res[0]['订单ID']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res);
		if($cdata){
			cache_write('kcdata',$cdata,'jy_kuaijiefee');
			exi('导入成功（【订单ID】重复的会自动忽略）！','','/order.cdata/');
		}else{
			exi('导入成功！','','/order.list/');
		}
	}
	public function csv_add($rows,$xlsx=0){
		$cdata=array();
		foreach($rows as $item2){
			if(!$item2['订单ID']){
				//exi('有数据不存在订单ID！','error');
			}
			$item2['下单时间']=trim($item2['下单时间']);
			$daytime=strtotime($item2['下单时间']);
		}
		foreach($rows as $item){
			if(!$item['核销门店ID'] && !$item['订单ID']){
				continue;
				//exi('有数据不存在上课日期！','error');
			}
			$item['商家id']=trim($item['核销门店ID']);
			$item['下单时间']=trim($item['下单时间']);
			$item['商品id']=trim($item['商品id']);
			$item['订单ID']=trim($item['订单ID']);
			$item['核销员快手ID']=trim($item['核销员快手ID']);
			$item['用户支付金额']=trim($item['用户支付金额']);
			$daytime=strtotime($item['下单时间']);
			$api_data=array(
				'pingfee'=>trim($item['平台服务费（含支付通道费）']),
				'dafee'=>trim($item['达人佣金']),
				'dafeerate'=>trim($item['达人佣金率']),
				'fuwufee'=>trim($item['商家服务商佣金']),
			);
			$data=array(
				'onepluginname'=>'jy_kuaijiefee',
				'plugin_name'=>'jy_kuaijiefee',
				'api_id'=>$item['订单ID'],
				'ordersn'=>'ks'.$item['订单ID'],
				'ispay'=>1,
				'status'=>3,
				'paytype'=>'down',
				'paytime'=>$daytime,
				'createtime'=>$daytime,
				'price'=>$item['用户支付金额'],
				'api_data'=>ijson_encode($api_data)
			);
			if($item['商家id']){
				$merch=pdo_get('jy_merch_user',array('dd_shop_id'=>$item['商家id'],'uniacid'=>$_SESSION['uniacid']),array('id','pluginname'));
				$data['merchid']=$merch['id'];
				if(!$data['merchid']){
					$setting['ks_id']=$item['核销员快手ID'];
					$insertdata=array(
						'merchname'=>trim($item['核销门店名称']),
						'pluginname'=>'jy_kuaijiefee',
						'setting'=>ijson_encode($setting),
						'status'=>0,
						'jointime'=>time(),
					);
					$insertdata['uniacid']=$_SESSION['uniacid'];
					$insertdata['dd_shop_id']=$item['商家id'];
					pdo_insert('jy_merch_user',$insertdata);
					$data['merchid']=pdo_insertid();
				}elseif($merch['pluginname']!='jy_kuaijiefee'){
					pdo_update('jy_merch_user',array('pluginname'=>'jy_kuaijiefee'),array('id'=>$data['merchid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
			if($item['商品id']){
				$goodsid=pdo_getcolumn('jy_weishop_goods',array('api_id'=>trim($item['商品id']),'uniacid'=>$_SESSION['uniacid']),'id');
				if(!$goodsid){
					$insertdata=array(
						'title'=>trim($item['商品名称']),
						'price'=>trim($item['商品原价']),
						'api_id'=>trim($item['商品id']),
					);
					$insertdata['uniacid']=$_SESSION['uniacid'];
					$insertdata['pluginname']='jy_kuaijiefee';
					$insertdata['onepluginname']='jy_kuaijiefee';
					$insertdata['merchid']=$data['merchid'];
					$insertdata['createtime']=TIMESTAMP;
					pdo_insert('jy_weishop_goods',$insertdata);
					$goodsid=pdo_insertid();
				}
			}
			//订单重复
			$isadd=pdo_get('jy_weishop_order',array('plugin_name'=>'jy_kuaijiefee','onepluginname'=>'jy_kuaijiefee','api_id'=>$data['api_id'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if($isadd['id']){
				$cdata[]=array('merchid'=>$item['商家id'],'goodsid'=>$item['商品id'],'orderid'=>$data['api_id']);
				pdo_update('jy_weishop_order',array('price'=>$data['price'],'paytime'=>$data['paytime'],'createtime'=>$data['createtime'],'merchid'=>$data['merchid'],'api_data'=>ijson_encode($api_data)),array('uniacid'=>$_SESSION['uniacid'],'id'=>$isadd['id']));
				continue;
			}
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_order',$data);
			$orderid=pdo_insertid();
			$gooddata=array(
				'plugin_name'=>'jy_kuaijiefee',
				'goodsid'=>$goodsid,
				'orderid'=>$orderid,
				'merchid'=>$data['merchid'],
				'total'=>1,
				'ispay'=>1,
				'status'=>3,
				'createtime'=>$daytime,
				'price'=>trim($item['商品原价']),
			);
			$gooddata['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_order_goods',$gooddata);
		}
		return $cdata;
	}
}
?>